package com.anche.cywebnew.Service.businessManager.impl;

import com.alibaba.fastjson.JSONObject;
import com.anche.cywebnew.DAO.businessManager.CyDataExceptionInfoMapper;
import com.anche.cywebnew.DAO.businessManager.CyDataOBDMapper;
import com.anche.cywebnew.DAO.configManager.SysOptionMapper;
import com.anche.cywebnew.Pojo.businessManager.CyDataExceptionInfo;
import com.anche.cywebnew.Pojo.businessManager.CyDataOBDInfo;
import com.anche.cywebnew.Pojo.businessManager.CyDataVideoInfo;
import com.anche.cywebnew.Service.businessManager.CyDataExceptionService;
import com.anche.cywebnew.Utils.http.HttpClientResult;
import com.anche.cywebnew.Utils.http.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class CyDataExceptionServiceImpl implements CyDataExceptionService {


    @Autowired
    private CyDataOBDMapper cyDataOBDMapper;
    @Autowired
    private CyDataExceptionInfoMapper cyDataExceptionInfoMapper;
    @Autowired
    private SysOptionMapper sysOptionMapper;

    @Override
    public CyDataExceptionInfo getExceptionInfoByCylsh(String cylsh) {
        CyDataExceptionInfo cyDataExceptionInfo = cyDataExceptionInfoMapper.selectByCylsh(cylsh);
        if (cyDataExceptionInfo == null) {
            cyDataExceptionInfo = getExceptionInfo(cylsh);
            if (cyDataExceptionInfo != null) {
                cyDataExceptionInfoMapper.insertCyDataException(cyDataExceptionInfo);
            }
        }
        return cyDataExceptionInfo;
    }

    private CyDataExceptionInfo getExceptionInfo(String cylsh) {
        Map<String, String> map = new HashMap<>();
        map.put("cylsh", cylsh);
        try {
            String isInvokeCyData = sysOptionMapper.getOptionValue("系统配置", "isInvokeCyData", "1");
            if (isInvokeCyData != null && isInvokeCyData.equals("1")) {
                String CyDataAddr = sysOptionMapper.getOptionValue("系统配置", "CyDataAddr", "1");
                HttpClientResult clientResult = HttpClientUtils.doGet(CyDataAddr + "/ywyj/ywyjInfo", map);
                String content = clientResult.getContent();
                Map<String, String> result = JSONObject.parseObject(content, Map.class);
                if (result.get("code").equals("1")) {
                    CyDataExceptionInfo cyDataExceptionInfo = new CyDataExceptionInfo();
                    cyDataExceptionInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    cyDataExceptionInfo.setCylsh(cylsh);
                    cyDataExceptionInfo.setCqyj(result.get("cqyj"));
                    cyDataExceptionInfo.setFgzsjyj(result.get("gzsjyj"));
                    cyDataExceptionInfo.setTbcsyj(result.get("tbyj"));
                    cyDataExceptionInfo.setKzyj(result.get("kzyj"));
                    cyDataExceptionInfo.setFjcsyj(result.get("fjyj"));
                    cyDataExceptionInfo.setHmdyj(result.get("hmdyj"));
                    String ExceptionItem = sysOptionMapper.getOptionValue("系统配置", "ExceptionItem", "1");
                    if (ExceptionItem.contains("4")) {
                        CyDataOBDInfo obdInfo = cyDataOBDMapper.selectByCylsh(cylsh);
                        CyDataVideoInfo videoInfo = new CyDataVideoInfo();
                        videoInfo.setCylsh(cylsh);
                        videoInfo.setCyxm("P1");
                        String gzgf = "";
                        if (obdInfo == null) {
                            gzgf += "异常：查验过程未使用OBD设备。";
                        } else {
                            gzgf += "正常：查验过程正常上传OBD数据。";
                        }
                        if (videoInfo == null) {
                            gzgf += "异常：查验过程未使用执法记录仪。";
                        } else {
                            gzgf += "正常：查验过程使用执法记录仪。";
                        }
                        cyDataExceptionInfo.setGzgfyj(gzgf);
                    } else {
                        cyDataExceptionInfo.setGzgfyj("正常：未启用工作规范预警。");
                    }
                    if (content.contains("异常") || (cyDataExceptionInfo.getGzgfyj() != null
                            && cyDataExceptionInfo.getGzgfyj().contains("异常"))) {
                        cyDataExceptionInfo.setYjzt("0");
                    } else {
                        cyDataExceptionInfo.setYjzt("1");
                    }
                    return cyDataExceptionInfo;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
